package org.afs.gescol.core.dao.implementations;

import java.util.List;

import org.afs.gescol.core.dao.interfaces.GlobalParamsDao;
import org.afs.gescol.core.dto.time.YearSplitCriteriaDto;
import org.afs.gescol.core.model.params.Durations;
import org.afs.gescol.core.model.params.HourlyTime;
import org.afs.gescol.core.model.params.YearSplit;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.scub.foundation.framework.core.dao.implementations.HibernateDaoBaseImplementation;

/**
 * GlobalParamsDao Implementation.
 * @author Kobla GBENYO (kobla.gbenyo@gmail.com)
 */
public class GlobalParamsDaoImpl extends HibernateDaoBaseImplementation implements GlobalParamsDao {

    @SuppressWarnings("unchecked")
    @Override
    public List<YearSplit> getYearSplitByCriteria(YearSplitCriteriaDto criteria) {
        final Criteria searCriteria = createCriteria(YearSplit.class);
        if (criteria.getCycleId() != null) {
            searCriteria.add(Restrictions.eq("cycle.id", criteria.getCycleId()));
        }
        if (criteria.getYearId() != null) {
            searCriteria.add(Restrictions.eq("year.id", criteria.getYearId()));
        }
        if (criteria.getStatusId() != null) {
            searCriteria.add(Restrictions.eq("status.id", criteria.getStatusId()));
        }
        return searCriteria.list();
    }

    @Override
    public Durations getDurationsByCycle(Long cycleId) {
        final Criteria criteria = createCriteria(Durations.class);
        criteria.add(Restrictions.eq("cycle.id", cycleId));
        return (Durations) criteria.uniqueResult();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<HourlyTime> getHourlyTime(Long cycleId) {
        final Criteria criteria = createCriteria(HourlyTime.class);
        criteria.add(Restrictions.eq("cycle.id", cycleId));
        return criteria.list();
    }

}
